/*
 * @Description:
 * @Author: dh
 * @Date: 2022-04-11 10:54:44
 * @LastEditors: dh
 * @LastEditTime: 2025-09-11 11:31:34
 */
var arr = [1111, 2222, 3333]

/* TODO 自定义 filter ================================================================================ */
Array.prototype.myFilter = function (fn, context) {
	if (Object.prototype.toString.call(this).slice(8, -1) === 'Array') {
		let arr = []
		for (let i = 0; i < this.length; i++) {
			let res = fn.call(context, this[i], i, this)
			if (res) arr.push(this[i])
		}
		return arr
	}
}
var newArr = arr.myFilter(function (v) {
	return v >= 3333
})
// console.log(newArr); [3333]

/* TODO 自定义 some ================================================================================ */
Array.prototype.mySome = function (fn, context) {
	if (Object.prototype.toString.call(this).slice(8, -1) === 'Array') {
		for (let i = 0; i < this.length; i++) {
			let res = fn.call(context, this[i], i, this)
			if (res) return true
		}
		return false
	}
}

let res = arr.mySome(function (v) {
	return v >= 2222
})
// console.log(res); true

/* TODO 自定义 map ================================================================================ */
Array.prototype.myMap = function (fn, context) {
	if (typeof fn !== 'function') {
		throw new TypeError(`${fn} is not a function`)
	}
	let arr = []
	for (let i = 0; i < this.length; i++) {
		arr.push(fn.call(context, this[i], i, this))
	}
	return arr
}

var newArr = arr.myMap(v => (v += 1))
// console.log(newArr);  //[1112, 2223, 3334]

/* TODO 自定义 reduce ============================================================================== */
Array.prototype.myReduce = function (func, initialValue) {
	var base = typeof initialValue === 'undefined' ? this[0] : initialValue
	var index = typeof initialValue === 'undefined' ? 1 : 0
	for (let i = index; i < this.length; i++) {
		base = func(base, this[i], i, this)
	}
	return base
}
// var total = arr.myReduce((a, b, i) => {
//     return b + a
// }, 10)
// console.log(total); //6666

/* TODO 自定义 flat ================================================================================ */
var arr = [1, [2, 2, [3, 3, 3, [4, 4, 4, 4, [5]]]]]
// for循环+ 递归
Array.prototype.myFlat = function (num = 1) {
	let newArr = []
	for (let i = 0; i < this.length; i++) {
		if (Array.isArray(this[i]) && num > 0) {
			newArr = newArr.concat(this[i].myFlat(num - 1))
		} else {
			newArr.push(this[i])
		}
	}
	return newArr
}
// while循环
Array.prototype.myFlat = function (num) {
	let res = this
	while (num > 0) {
		let arr = []
		for (let i = 0; i < res.length; i++) {
			if (Array.isArray(res[i])) {
				arr.push(...res[i])
			} else {
				arr.push(res[i])
			}
		}
		num--
		res = arr
	}

	return res
}
// reduce + 递归
Array.prototype.myFlat = function (depth = 1) {
	if (depth > 0) {
		return this.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? cur.myFlat(depth - 1) : cur), [])
	}
	return this.slice()
}
// console.log(arr.myFlat(Infinity));

/* TODO 自定义 indexOf lastIndexOf================================================================================ */
Array.prototype.myIndexOf = function (searchElement, fromIndex) {
	if (fromIndex === undefined) {
		fromIndex = 0
	} else {
		fromIndex = fromIndex < 0 ? Math.max(this.length + fromIndex, 0) : fromIndex
	}

	for (let i = fromIndex; i < this.length; i++) {
		if (this[i] === searchElement) {
			return i
		}
	}
	return -1
}

Array.prototype.myLastIndexOf = function (searchElement, fromIndex) {
	fromIndex = fromIndex !== undefined ? Math.min(fromIndex, this.length - 1) : arr.length - 1
	for (let i = fromIndex; i >= 0; i--) {
		if (searchElement === this[i]) {
			return i
		}
	}
	return -1
}

/* TODO 自定义 findIndex myFindLastIndex ================================================================================ */
Array.prototype.myFindIndex = function (fn) {
	for (let i = 0; i < this.length; i++) {
		if (fn.call(null, this[i], i, this)) {
			return i
		}
	}
	return -1
}
Array.prototype.myFindLastIndex = function (callback, context) {
	for (let index = this.length - 1; index >= 0; index--) {
		const value = this[index]
		if (callback.call(context, value, index, this)) {
			return index
		}
	}
	return -1
}
// console.log([1, 2, 3].myFindLastIndex(v => v > 2))

/* TODO 自定义 find findLast================================================================================ */
Array.prototype.myFind = function (callback, context) {
	for (let i = this.length - 1; i >= 0; i--) {
		if (callback.call(context, this[i], i, this)) {
			return this[i]
		}
	}
	return undefined
}
Array.prototype.myFindLast = function (callback, context) {
	for (let index = this.length - 1; index >= 0; index--) {
		const value = this[index]
		if (callback.call(context, value, index, this)) {
			return value
		}
	}
	return undefined
}
// console.log([1, 2, 3].myFindLast(v => v > 2))
